<?php
/**
 * 此文件为超级装载器
 *
 */
class Loader{
    
    private static $_objects = array();
    private static $_configs = array();
    private static $_helpers = array();
    private static $_models  = array();
    private static $_databases = array();
    private static $_view    = null;
    
    static function &instance(){
        static $instance;
        if (is_null($instance)) $instance = new loader();
        return $instance;
    }
    /**
     * 装载库文件
     */
    static function &lib($class){
        $class = ucfirst($class);
        if (isset(self::$_objects[$class])){
            return self::$_objects[$class];
        }
        if(file_exists(APPBASE.'Libraries/MY_'.$class.'.php')){
            require(APPBASE.'Libraries/MY_'.$class.'.php');
            $name = 'MY_'.$class;
            self::$_objects[$class] = new $name();
            return self::$_objects[$class];
        }else{
            if (file_exists(SYSBASE.'Libraries/SP_'.$class.'.php')){
                require(SYSBASE.'Libraries/SP_'.$class.'.php');
                $name = 'SP_'.$class;
                self::$_objects[$class] = new $name();
                return self::$_objects[$class];
            }else{
                showError(sprintf(__T("Class SP_%s is not valid!"),$class));
            }
        }
    }
    /**
     * 装载模型
     */
    static function &model($modelName){
        if(!isset(self::$_models[$modelName])){
            $modelClass = $modelName.'Model';
            if(defined("MODELDIR") && MODELDIR){
                 if(file_exists(MODELDIR.$modelClass.'.php')) {
                     require(MODELDIR.$modelClass.'.php');
                     self::$_models[$modelName] = new $modelClass;
                     return self::$_models[$modelName];
                 }
            }
            $modelPath = APPBASE.'Models/'.$modelClass.'.php';
            if(file_exists($modelPath)) 
                require($modelPath);
            else 
                showError('Model '.$modelName.' don\'t exist!');

            self::$_models[$modelName] = new $modelClass;
        }
        return self::$_models[$modelName];
    }
    /**
     * 装载数据库
     */
    static function &database($dbstr,$config = ''){
        if (!isset(self::$_databases[$dbstr])){
            if(is_array($config)){
                $DB_config = $config;
            }else{
                $Config =& self::config();
                $DB_config = $Config['database'][$dbstr];
            }
            if (file_exists(SYSBASE.'Database/DB.php')){
                require_once(SYSBASE.'Database/DB.php');
                
                self::$_databases[$dbstr] = new DB($DB_config);
            }else{
                showError(__T("Class DB is not valid!"));
            }
        }
        return self::$_databases[$dbstr];
    }
    /**
     * 装载视图
     */
    static function view($tplFile,$isDisplay = true){
        if(!self::$_view){
            $Config =& self::config();
            $viewEngineObj = 'View_'.ucfirst($Config['view_engine']);
            $viewEngineFile = SYSBASE.'Libraries/View/'.ucfirst($Config['view_engine']).'.php';
            require($viewEngineFile);
            self::$_view = new $viewEngineObj();
        }

        if($isDisplay){
            self::$_view->display($tplFile);
            return true;
        }else{
            return self::$_view->fetch($tplFile);
        }
    }
    /**
     * 装载配置
     */
    static function &config($name = 'conf'){
        if ( !isset(self::$_configs[$name])){
            if ( ! file_exists(APPBASE."Config/{$name}.php")){
                showError(sprintf(__T("The configuration file %s.php does not exist."),$name));
            }
            require(APPBASE."Config/{$name}.php");

            if ( ! isset($CONFIG) || ! is_array($CONFIG)){
                showError(__T("Your config file does not appear to be formatted correctly."));
            }

            self::$_configs[$name] =& $CONFIG;
        }
        return self::$_configs[$name];
    }
    /**
     * 载入helper
     *
     */
    static function helper($helper){
        if(isset(self::$_helpers[$helper])){
            return true;
        }

        $helperPath = SYSBASE.'Helpers/'.$helper.'.php';
        $appHelperPath = APPBASE.'Helpers/'.$helper.'.php';
        
        if(file_exists($appHelperPath)){
            self::$_helpers[$helper] = true;
            require($appHelperPath);
        }else{
            if(file_exists($helperPath)){
                self::$_helpers[$helper] = true;
                require($helperPath);
            }else{
                return false;
            }
        }
        return true;
    }
}
